package com.sky.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Lat_Lng {
    // 地球平均半径（单位：公里）
    private static final double EARTH_RADIUS_KM = 6371.0;

    private double lat;
    private double lng;

    public double distance(Lat_Lng lat_lng){
        double lat1Rad = Math.toRadians(this.lat);
        double lon1Rad = Math.toRadians(this.lng);
        double lat2Rad = Math.toRadians(lat_lng.lat);
        double lon2Rad = Math.toRadians(lat_lng.lng);

        // 经纬度差值
        double deltaLat = lat2Rad - lat1Rad;
        double deltaLon = lon2Rad - lon1Rad;

        // Haversine 公式计算
        double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2)
                + Math.cos(lat1Rad) * Math.cos(lat2Rad)
                * Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        // 计算距离
        return EARTH_RADIUS_KM * c;
    }
}
